package com.evan.review.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.evan.review.R;

/**
 * 带有颜色的Fragment
 * 用于回退栈演示，可设置不同的背景颜色
 */
public class ColorFragment extends Fragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_COLOR = "color";
    private static final String ARG_ID = "id";
    
    private String title;
    private int color;
    private int fragmentId;
    
    private FragmentCallback callback;
    
    /**
     * Fragment回调接口
     */
    public interface FragmentCallback {
        void onFragmentEvent(String event);
    }
    
    /**
     * 设置回调
     */
    public void setCallback(FragmentCallback callback) {
        this.callback = callback;
    }
    
    /**
     * 工厂方法创建Fragment实例
     */
    public static ColorFragment newInstance(String title, int color, int id) {
        ColorFragment fragment = new ColorFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putInt(ARG_COLOR, color);
        args.putInt(ARG_ID, id);
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 获取参数
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE, "未命名");
            color = getArguments().getInt(ARG_COLOR, 0xFFCCCCCC);
            fragmentId = getArguments().getInt(ARG_ID, 0);
        }
        
        logEvent("onCreate()");
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        logEvent("onCreateView()");
        return inflater.inflate(R.layout.fragment_color, container, false);
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        logEvent("onViewCreated()");
        
        // 设置标题和背景颜色
        TextView tvTitle = view.findViewById(R.id.tv_title);
        View colorView = view.findViewById(R.id.color_view);
        Button btnNavigateBack = view.findViewById(R.id.btn_navigate_back);
        
        tvTitle.setText(title + " (ID: " + fragmentId + ")");
        colorView.setBackgroundColor(color);
        
        // 设置按钮点击事件
        btnNavigateBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logEvent("点击了[返回]按钮");
                
                // 弹出回退栈
                if (getParentFragmentManager().getBackStackEntryCount() > 0) {
                    getParentFragmentManager().popBackStack();
                } else {
                    logEvent("回退栈为空，无法返回");
                }
            }
        });
    }
    
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        logEvent("onAttach()");
    }
    
    @Override
    public void onStart() {
        super.onStart();
        logEvent("onStart()");
    }
    
    @Override
    public void onResume() {
        super.onResume();
        logEvent("onResume()");
    }
    
    @Override
    public void onPause() {
        super.onPause();
        logEvent("onPause()");
    }
    
    @Override
    public void onStop() {
        super.onStop();
        logEvent("onStop()");
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        logEvent("onDestroyView()");
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        logEvent("onDestroy()");
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        logEvent("onDetach()");
        callback = null;
    }
    
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        logEvent("onHiddenChanged() - hidden: " + hidden);
    }
    
    /**
     * 记录事件并通过回调通知
     */
    private void logEvent(String event) {
        String message = "[" + title + " #" + fragmentId + "] " + event;
        
        if (callback != null) {
            callback.onFragmentEvent(message);
        }
    }
} 